<template>
  <div :class="{ hasTagsView: needTagsView }" class="main-content">
    <div class="mb-[10px]">
      <a-card hoverable>
        <a-flex gap="small" justify="space-between">
          <a-form ref="queryFormRef" :model="queryParams" layout="inline">
            <a-form-item label="登录地址" name="ipaddr">
              <el-input v-model="queryParams.ipaddr" clearable placeholder="请输入登录地址"
                        @change="handleQuery" @keyup.enter="handleQuery" />
            </a-form-item>
            <a-form-item label="用户名称" name="userName">
              <el-input v-model="queryParams.userName" clearable placeholder="请输入用户名称"
                        @change="handleQuery" @keyup.enter="handleQuery" />
            </a-form-item>
          </a-form>
          <a-flex justify="end">
            <el-button icon="Search" :loading="loading" round plain type="primary"
                       @click="handleQuery">搜索
            </el-button>
            <el-button icon="RefreshRight" :loading="loading" round
                       @click="resetQuery">重置
            </el-button>
          </a-flex>
        </a-flex>
      </a-card>
    </div>
    <a-card ref="cardRef" class="card-content" hoverable>
      <a-spin :spinning="loading" size="large">
      <el-table
        :data="onlineList.slice((queryParams.pageNum - 1) * queryParams.pageSize, queryParams.pageNum * queryParams.pageSize)"
          :height="tableHeight+15"
        style="width: 100%"
      >
          <el-table-column align="center" label="序号" type="index" min-width="50">
          <template #default="scope">
            <span>{{ (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1 }}</span>
          </template>
        </el-table-column>
          <el-table-column :show-overflow-tooltip="true" align="center" label="会话编号" min-width="200"
                           prop="tokenId" />
          <el-table-column :show-overflow-tooltip="true" align="center" label="登录名称" min-width="150"
                           prop="userName" />
          <el-table-column :show-overflow-tooltip="true" align="center" label="客户端" min-width="150"
                           prop="clientKey" />
          <el-table-column align="center" label="设备类型" min-width="100">
          <template #default="scope">
            <dict-tag :options="sys_device_type" :value="scope.row.deviceType" />
          </template>
        </el-table-column>
          <el-table-column :show-overflow-tooltip="true" align="center" label="所属部门" min-width="150"
                           prop="deptName" />
          <el-table-column :show-overflow-tooltip="true" align="center" label="主机" min-width="150" prop="ipaddr" />
          <el-table-column :show-overflow-tooltip="true" align="center" label="登录地点" min-width="150"
                           prop="loginLocation" />
          <el-table-column :show-overflow-tooltip="true" align="center" label="操作系统" min-width="250" prop="os" />
          <el-table-column :show-overflow-tooltip="true" align="center" label="浏览器" min-width="150" prop="browser" />
          <el-table-column align="center" label="登录时间" prop="loginTime" width="180">
          <template #default="scope">
              <span>{{ parseTime(scope.row.loginTime) }}</span>
          </template>
        </el-table-column>
          <el-table-column align="center" fixed="right" label="操作"
                           min-width="80">
          <template #default="scope">
            <el-tooltip content="强退" placement="top">
                <el-button v-hasPermi="['monitor:online:forceLogout']" circle plain icon="Delete" type="danger"
                           @click="handleForceLogout(scope.row)">
              </el-button>
            </el-tooltip>
          </template>
        </el-table-column>
          <template #empty>
            <a-empty />
          </template>
      </el-table>
      </a-spin>
    </a-card>
    <a-card hoverable ref="paginationRef" v-show="total>0">
      <pagination v-model:limit="queryParams.pageSize" v-model:page="queryParams.pageNum"
                  :total="total" @pagination="getList" />
    </a-card>
  </div>
</template>

<script setup name="Online" lang="ts">
import { forceLogout, list as initData } from '@/api/monitor/online';
import { OnlineQuery, OnlineVO } from '@/api/monitor/online/types';
import api from '@/api/system/user';
import { to } from 'await-to-js';

import { useElTableHeight } from '@/utils/useElTableHeight';
import useSettingsStore from '@/store/modules/settings';
import { parseTime } from '../../../utils/ruoyi';
import { ReloadOutlined, SearchOutlined } from '@ant-design/icons-vue';

const settingsStore = useSettingsStore();
const needTagsView = computed(() => settingsStore.tagsView);
/*计算表格高度*/
const cardRef = ref<HTMLElement | null>(null);
const headerRef = ref<any>(null);
const paginationRef = ref<any>(null);
const { tableHeight } = useElTableHeight(cardRef, [paginationRef]);

const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const { sys_device_type } = toRefs<any>(proxy?.useDict('sys_device_type'));

const onlineList = ref<OnlineVO[]>([]);
const loading = ref(true);
const total = ref(0);

const queryFormRef = ref<ElFormInstance>();

const queryParams = ref<OnlineQuery>({
  pageNum: 1,
  pageSize: 20,
  ipaddr: '',
  userName: ''
});

/** 查询登录日志列表 */
const getList = async () => {
  loading.value = true;
  const res = await initData(queryParams.value);
  onlineList.value = res.rows;
  total.value = res.total;
  loading.value = false;
};
/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
};
/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value?.resetFields();
  handleQuery();
};
/** 强退按钮操作 */
const handleForceLogout = async (row: OnlineVO) => {
  const [err] = await to(proxy?.$modal.confirm('是否确认强退名称为"' + row.userName + '"的用户?') as any);
  if (!err) {
    await forceLogout(row.tokenId);
    await getList();
    proxy?.$modal.notifySuccess('删除成功');
  }
};

onMounted(() => {
  getList();
});
</script>
